package org.demo.mainframe.fx.handler;

import javafx.event.EventHandler;
import javafx.event.EventType;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import jfxtras.labs.scene.control.window.Window;

public class WindowToFrontHandler implements EventHandler<MouseEvent> {
	
	public WindowToFrontHandler(){}

	public void handle(MouseEvent mEvent) {
		
		MouseButton button = mEvent.getButton();
		Object source = mEvent.getSource();
		if(button == MouseButton.PRIMARY){
			if(source instanceof Node){
				Parent p1 = ((Node) source).getParent();
				if(p1 instanceof Window){
					toFront((Window)p1);
					return ;
				}
				Parent p2 = p1.getParent();
				if(p2 instanceof Window){
					toFront((Window)p2);
					return ;
				}
				
				Parent p3 = p2.getParent();
				if(p3 instanceof Window){
					toFront((Window)p3);
					return ;
				}
				
				Parent p4 = p3.getParent();
				if(p4 instanceof Window){
					toFront((Window)p4);
					return ;
				}
			}
		}
	}

	private void toFront(Window w) {
		w.toFront();
	}

}
